Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Collection<T>: Validate parameters in the public methods #23166

Merged
merged 1 commit into from
Mar 15, 2019

Conversation

justinvp
Copy link

@justinvp justinvp commented Mar 10, 2019

I was looking over the new Range methods on Collection<T> (added in #23018) and it occurred to me that for all existing methods, parameter validation is done in the public methods before calling the protected virtual methods. This changes the new Range methods to do the same.

cc: @ahoefling, @ahsonkhan, @safern

I was looking over the new Range methods on `Collection<T>` and it occurred to me that for all existing methods, parameter validation is done in the *public* methods before calling the *protected virtual* methods. This changes the new Range methods to do the same.
@filipnavara
Copy link
Member

filipnavara commented Mar 10, 2019

Sounds like a good idea for a couple of reasons:

  • People overriding the methods don't need to perform the error checking themselves. If someone already did that then it won't break existing code. The parameter checks look inexpensive and quite generic.
  • The public method may get inlined and the checks could be eliminated altogether for the particular call site.

@SkyeHoefling
Copy link

I agree, this will solve some problems we are already having in the CoreFX project.


if (collection == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no list argument to this method, but the exception is going to be new ArgumentNullException("list"). The ExceptionArgument.list should be changed to ExceptionArgument.collection.


if ((uint)index > (uint)items.Count)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exception message is "Index must be within the bounds of the List.". The code consuming this isn't using "List", though.


if (index > items.Count - count)
{
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception message here is "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection"... but the arguments to this method are "index" and "count", not "offset" and "length"... are we ok with that mismatch?


if (index > items.Count - count)
{
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as earlier.


if (collection == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as earlier.

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual changes here LGTM. But some of the previous code that's being moved is suspect, in particular with regards to the contents of the exceptions being thrown. Let's merge this, but then we should follow up to get those exceptions fixed as is appropriate.

@stephentoub stephentoub merged commit 5231179 into dotnet:master Mar 15, 2019
@justinvp
Copy link
Author

Let's merge this, but then we should follow up to get those exceptions fixed as is appropriate.

@ahoefling, are you planning to follow-up with a PR?

Copy link
Member

@safern safern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @justinvp

@SkyeHoefling
Copy link

@justinvp I have some time today and will work on a PR for this based on the feedback above

picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…clr#23166)

I was looking over the new Range methods on `Collection<T>` and it occurred to me that for all existing methods, parameter validation is done in the *public* methods before calling the *protected virtual* methods. This changes the new Range methods to do the same.

Commit migrated from dotnet/coreclr@5231179
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants